package com.socialbank.web.util;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.codehaus.jackson.map.ser.std.ToStringSerializer;

public class SocialBankJsonUtil {

	@SuppressWarnings("deprecation")
	public static String listMapToJsonString(List<Map<String, Object>> manyContents) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(manyContents);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
	
	@SuppressWarnings("deprecation")
	public static String mapToJsonString(Map<String, Object> manyContents) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.registerModule(simpleModule);
		objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(manyContents);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
	
	@SuppressWarnings("deprecation")
	public static String mapToJsonStringNotLong(Map<String, Object> manyContents) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		//SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		//simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		//simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		//objectMapper.registerModule(simpleModule);
		objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(manyContents);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
	@SuppressWarnings("deprecation")
	public static String objectToJsonString(Object object) {
		String jsonArray = "";
		ObjectMapper objectMapper = new ObjectMapper();
		SimpleModule simpleModule = new SimpleModule("sola", Version.unknownVersion());
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		objectMapper.registerModule(simpleModule);
		objectMapper.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		try {
			jsonArray = objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
	
}
